package com.aispeech.rcprvd.provider;

import android.app.Application;
import android.bluetooth.BluetoothDevice;
import android.util.Log;

import com.aispeech.rcprvd.RcPrvdListener;
import com.aispeech.rcprvd.jni.RcNativeProcessJni;
import com.aispeech.rcprvd.profile.JinjuBleManager;
import com.aispeech.rcprvd.provider.base.GattServerDataProvider;
import com.aispeech.rcprvd.utils.AudioFileWrite;
import com.aispeech.rcprvd.utils.DataParseUtils;
import com.aispeech.rcprvd.utils.RcPrvdConst;
import com.aispeech.rcprvd.utils.RemoteDeviceUtils;

import java.io.File;

import androidx.annotation.NonNull;
import no.nordicsemi.android.ble.callback.DataReceivedCallback;
import no.nordicsemi.android.ble.data.Data;
import no.nordicsemi.android.ble.livedata.ObservableBleManager;

public class JinJuGattDataProvider extends GattServerDataProvider {
    private final String TAG = "JinJuGattDataProvider";

    private final String JINJU_VOICE_KEY_CODE_1 = "0221";
    private final String JINJU_VOICE_KEY_CODE_3 = "02cf";
    private final String JINJU_VOICE_KEY_UP = "cccc";

    private final String JINJU_BLE_NAME = "语音助手";
    private BluetoothDevice mDevice;
    private JinjuBleManager mBleManager;
    private RcPrvdListener mListener;

    private final boolean DEBUG = true;
//    private final boolean DEBUG = false;

    private AudioFileWrite mAudioFile;


    private DataReceivedCallback mVoiceKeyEventCallback = new DataReceivedCallback() {
        @Override
        public void onDataReceived(@NonNull BluetoothDevice device, @NonNull Data data) {
            try {
                if (null != mListener) {
                    byte[] value = data.getValue();
                    if (null != value) {
                        String skeycode = DataParseUtils.bytes2String16(value);
                        Log.d(TAG, "mVoiceKeyEventCallback: " + skeycode);
                        if (JINJU_VOICE_KEY_CODE_1.equals(skeycode)) {
                            mListener.onKeyEvent(Integer.parseInt(JINJU_VOICE_KEY_CODE_1), 1);
                        } else if (JINJU_VOICE_KEY_UP.equals(skeycode)) {
                            mListener.onKeyEvent(Integer.parseInt(JINJU_VOICE_KEY_CODE_1), 0);
                        }
                    }
                } else {
                    Log.w(TAG, "mVoiceKeyEventCallback: listener is null");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };

    private final DataReceivedCallback mVoiceDataCallback = new DataReceivedCallback() {
        @Override
        public void onDataReceived(@NonNull BluetoothDevice device, @NonNull Data data) {
            byte[] buf = data.getValue();
            if (null != buf && getTargetBleManager() != null && getTargetBleManager().isConnected()) {
                if (DEBUG) {
                    mAudioFile.writeBuffer(buf);
                }
                RcNativeProcessJni.native_process_feed(getNativeProcessId(), buf, buf.length);
            }
        }
    };

    public JinJuGattDataProvider(Application context, RcPrvdListener listener) {
        super(context);
        mDevice = RemoteDeviceUtils.getTargetBondedBleDevice(context, JINJU_BLE_NAME);
        mBleManager = new JinjuBleManager(context, mVoiceKeyEventCallback, mVoiceDataCallback);
        mListener = listener;
        if (DEBUG) {
            String audioFilePath = context.getExternalCacheDir() + File.separator + "juxin.pcm";
            mAudioFile = new AudioFileWrite(context);
            mAudioFile.startWrite(audioFilePath);
        }
    }

    @Override
    public BluetoothDevice getTargetDevice() {
        return mDevice;
    }

    @Override
    public int getNativeProcessId() {
        return RcPrvdConst.RCDevSource.JINJU_BLE_GATT;
    }

    @Override
    public ObservableBleManager getTargetBleManager() {
        return mBleManager;
    }
}
